package com.didibigdata.servlets;

import com.didibigdata.Repositories.PointOutReadSizeRepository;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.HashSet;
import java.util.Set;

@WebServlet("/PointOutServletReadSizeExchange")
public class PointOutServletReadSizeExchange extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //日志
        Logger logger = LoggerFactory.getLogger(PointOutServletReadSizeExchange.class);
        logger.info("add PointOutServletReadSizeExchange logger successful!");

        String dayName = new String();
        String inOrOut = new String();
        request.setCharacterEncoding("utf-8");
        try {
            dayName = request.getParameter("dayName");
            inOrOut = request.getParameter("inOrOut");
            logger.info("dayName: [{}] and inOrOut: [{}]", dayName, inOrOut);
        } catch (Exception e) {
            logger.error("PointOutServletExchange request get fail!");
            e.printStackTrace();
        }

        String sqltimeTest = new String();
        if (dayName.isEmpty()) {
            sqltimeTest = "select * from t_didi201610_train_out_200m_day1_2to4 order by timestp";
        } else {
            sqltimeTest = "select * from t_didi201610_train_"+ inOrOut + dayName + " order by timestp";
        }

        PointOutReadSizeRepository pointOutReadSizeRepository = new PointOutReadSizeRepository();
        int orderSetSize = pointOutReadSizeRepository.getPointOutReadSize(sqltimeTest);
        ObjectMapper mapper = new ObjectMapper();
        String jsonSize = mapper.writeValueAsString(orderSetSize);
        response.setContentType("text/html; charset=utf-8");
        response.getWriter().write(jsonSize);
    }
}
